// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <cstdint>
#include <functional>
#include <iosfwd>
#include <memory>

#include <nebula/array/array_base.h>
#include <nebula/array/array_nested.h>

#include <turbo/utility/status.h>
#include <nebula/types/type.h>
#include <turbo/base/macros.h>

namespace nebula {

    /// \brief Compare two arrays, returning an edit script which expresses the difference
    /// between them
    ///
    /// An edit script is an array of struct(insert: bool, run_length: int64_t).
    /// Each element of "insert" determines whether an element was inserted into (true)
    /// or deleted from (false) base. Each insertion or deletion is followed by a run of
    /// elements which are unchanged from base to target; the length of this run is stored
    /// in "run_length". (Note that the edit script begins and ends with a run of shared
    /// elements but both fields of the struct must have the same length. To accommodate this
    /// the first element of "insert" should be ignored.)
    ///
    /// For example for base "hlloo" and target "hello", the edit script would be
    /// [
    ///   {"insert": false, "run_length": 1}, // leading run of length 1 ("h")
    ///   {"insert": true, "run_length": 3}, // insert("e") then a run of length 3 ("llo")
    ///   {"insert": false, "run_length": 0} // delete("o") then an empty run
    /// ]
    ///
    /// Diffing arrays containing nulls is not currently supported.
    ///
    /// \param[in] base baseline for comparison
    /// \param[in] target an array of identical type to base whose elements differ from base's
    /// \param[in] pool memory to store the result will be allocated from this memory pool
    /// \return an edit script array which can be applied to base to produce target
    TURBO_EXPORT
    turbo::Result<std::shared_ptr<StructArray>> Diff(const Array &base, const Array &target,
                                                     MemoryPool *pool = default_memory_pool());

    /// \brief visitor interface for easy traversal of an edit script
    ///
    /// visitor will be called for each hunk of insertions and deletions.
    TURBO_EXPORT turbo::Status VisitEditScript(
            const Array &edits,
            const std::function<turbo::Status(int64_t delete_begin, int64_t delete_end,
                                              int64_t insert_begin, int64_t insert_end)> &visitor);

    /// \brief return a function which will format an edit script in unified
    /// diff format to os, given base and target arrays of type
    TURBO_EXPORT turbo::Result<
            std::function<turbo::Status(const Array &edits, const Array &base, const Array &target)>>
    MakeUnifiedDiffFormatter(const DataType &type, std::ostream* os);

}  // namespace nebula
